<?php
namespace app\admin\builder;

use think\Cookie;

class AdminListBuilder extends AdminBuilder
{
    private $_title;
    private $_suggest;
    private $_keyList = array(); //表单头列名称
    private $_buttonList = array(); //button 组件列表
    private $_tableName ;
    private $_data = array(); //table数据
    private $_selectPostUrl;
    private $_doaction=[];
    private $_select = array();

    /**设置页面标题
     * @param $title 标题文本
     * @return AdminListBuilder
     */
    public function title($title)
    {
        $this->_title = $title;
        $this->meta_title = $title;
        return $this;
    }

    /**
     * suggest  页面标题边上的提示信息
     * @param $suggest
     * @return AdminListBuilder
     */
    public function suggest($suggest)
    {
        $this->_suggest = $suggest;
        return $this;
    }

    public function tableName($tablename)
    {
        $this->_tableName = $tablename ? $tablename : '';
        return $this;

    }


    /**
     * 筛选下拉选择url
     * @param $url string 已被U函数解析的地址
     * @return AdminListBuilder
     */
    public function setSelectPostUrl($url)
    {
        $this->_selectPostUrl = $url;
        return $this;
    }

    /**加入一个按钮
     * @param $title
     * @param $attr 按钮属性
     * @return AdminListBuilder
     */
    private function button($title, $attr, $url = '')
    {
        if ($url == '') {
            $url = $this->request->path();
        }
        $this->_buttonList[] = array('title' => $title, 'class' => $attr, 'url' => $url);
        return $this;
    }

    public function buttonNew($title = '新增', $attr = '', $url = '')
    {
        $attr = $attr . ' btn btn-secondary admin-list-button-link';

        return $this->button($title, $attr, $url);
    }

    public function buttonOpt($title = '管理', $attr = '', $url = '')
    {
        $attr = $attr . ' btn  btn-white admin-list-button-link';

        return $this->button($title, $attr, $url);
    }

    /**
     * 自定义post操作方式按钮
     * @param string $title
     * @param string $attr
     * @param string $url
     * @return AdminListBuilder
     */
    public function buttonOptPost($title = '自定义post', $attr = '', $url = '')
    {
        $attr = $attr . ' btn admin-list-button-post';

        return $this->button($title, $attr, $url);
    }


    /**
     * 自定义link操作方式按钮
     * @param string $title
     * @param string $attr
     * @param string $url
     * @return AdminListBuilder
     */
    public function buttonOptLink($title = '自定义link', $attr = '', $url = '')
    {
        $attr = $attr . ' btn btn-secondary admin-list-button-link';

        return $this->button($title, $attr, $url);
    }

    public function buttonDelete($title = '删除', $attr = '', $url = '')
    {
        $attr = $attr . ' btn btn-danger admin-list-button-post ';
        return $this->button($title, $attr, $url);
    }

    public function buttonRestore($title = '还原', $attr = '', $url = '')
    {
        $attr = $attr . ' btn btn-orange admin-list-button-post';
        return $this->button($title, $attr, $url);
    }

    public function select($key, $title = '筛选', $attr, $arrvalue = null)
    {
        if (empty($this->_selectPostUrl)) {
            $url = $this->request->pathinfo();
        } else {
            $url = $this->_selectPostUrl;
        }
        //选中状态赋予
        // 选择中值随便赋予为99999999

        if(Cookie::has($key) && Cookie::get($key)!=''){
            $selected=Cookie::get($key);
        }else{
            $selected=9999999;
        }

       // $selected = cookie($key)?cookie($key):99999999;

        $id = substr(md5(time() . $title), 0, 5); //生成下拉框id
        $attr = $attr . ' select-operate';
        $this->_select[] = array('key' => $key, 'title' => $title, 'class' => $attr, 'selected' => $selected, 'arrvalue' => $arrvalue, 'url' => $url, 'id' => $id);
        return $this;
    }

    private function key($name, $title, $type, $opt = null, $width = '150px')
    {
        $key = array('name' => $name, 'title' => $title, 'type' => $type, 'opt' => $opt, 'width' => $width);
        $this->_keyList[] = $key;
        return $this;
    }

    public function keyText($name, $title)
    {
        return $this->key($name, text($title), 'text');
    }

    public function keyStatus($name, $title)
    {
        return $this->key($name, text($title), 'status');
    }


    public function keyImg($name, $title)
    {
        return $this->key($name, text($title), 'image');
    }


    public function keyUrl($name, $title)
    {
        return $this->key($name, text($title), 'url');
    }


    public function keyUrlQiniu($name, $title)
    {
        return $this->key($name, text($title), 'urlQiniu');
    }



    /**
     * 私有化 方法 收集doaction 列表具体操作元素对象
     * @param $getUrl
     * @param $text
     * @param null $opt
     * @param string $type
     * @param string $title
     * @param string $changeTitle
     * @param string $statusName
     * @return $this
     * todo type :post(post请求) link(链接地址跳转) modal(模态窗口)
     */
    private function keyDoAction($getUrl, $text, $opt = null, $type = 'post', $title = '操作', $changeTitle = '', $statusName = '')
    {
        $doActionKey = null;
        foreach ($this->_keyList as $key) {
            if ($key['name'] === 'DOACTIONS') {
                $doActionKey = $key;
                break;
            }
        }
        if (!$doActionKey) {
            $this->key('DOACTIONS', $title, 'doaction');
        }
        $doaction = ['url' => $getUrl, 'text' => $text, 'opt' => $opt, 'type' => $type, 'changeTitle' => $changeTitle, 'statusName' => $statusName];
        $this->_doaction[] = $doaction;

        return $this;
    }

    /**
     * 发送post请求按钮，针对场景主要是 无参数 post请求 比如删除操作
     * @param $getUrl
     * @param string $text
     * @param $opt
     * @param string $type
     * @return AdminListBuilder
     */
    public function keyDoActionPost($getUrl, $text = 'Post操作', $opt, $type = 'post')
    {
        $opts['class'] = $opt;
        return $this->keyDoAction($getUrl, $text, $opts, $type);
    }

    /**
     * 页面跳转 id信息附加于url后
     * @param $getUrl
     * @param string $text
     * @param $opt
     * @param string $type
     * @return AdminListBuilder
     */
    public function keyDoActionLink($getUrl, $text = 'Link操作', $opt, $type = 'link')
    {
        $opts['class'] = $opt;
        return $this->keyDoAction($getUrl, $text, $opts, $type);
    }

    /**
     * 模态框操作
     * @param $getUrl
     * @param string $text
     * @param $opt
     * @param string $type
     * @return AdminListBuilder
     */
    public function keyDoActionModal($getUrl, $text = 'Modal操作', $opt, $type = 'modal')
    {
        $opts['class'] = $opt;
        return $this->keyDoAction($getUrl, $text, $opts, $type);
    }

    /**
     * 状态更换按钮
     * @param $getUrl
     * @param string $text
     * @param $opt
     * @param string $changeTitle
     * @param string $statusName
     * @return AdminListBuilder
     */
    public function keyDoActionPostTextChange($getUrl, $text = 'Modal操作', $changeTitle = '撤销发布', $statusName = 'status')
    {
        $type = 'postTextChange';
        return $this->keyDoAction($getUrl, $text, [], $type, '', $changeTitle, $statusName);
    }

    public function data($list)
    {
        $this->_data = $list;
        return $this;
    }

    public function displayList()
    {
        //显示页面
        $this->assign('title', $this->_title);
        $this->assign('suggest', $this->_suggest);
        $this->assign('keyList', $this->_keyList);
        $this->assign('buttonList', $this->_buttonList);
        $this->assign('list', $this->_data);
        $this->assign('_tableName', $this->_tableName);
        $this->assign('selects', $this->_select);
        $this->assign('selectPostUrl', $this->_selectPostUrl);
        $this->assign('doaction', $this->_doaction);

        //如果是选择返回数据的列表页就调用admin_solist模板文件，否则编译原有模板
        return parent::display('admin_list');
    }

    //将url字符串 通过内置url方法转化为标准url
    //$pattern 同一个控制器下 直接写action名称
    //跨模块 /module/controller/action
    private function createUrl($pattern)
    {
        //todo url 处理
        return url($pattern);
    }


}